<template>
  <basic-container>
    <avue-crud :option="option"
               :data="data"
               ref="crud"
               v-model="form"
               :table-loading="loading"
               :permission="permissionList"
               @row-del="rowDel"
               @row-update="rowUpdate"
               @row-save="rowSave"
               :before-open="beforeOpen"
               :page="page"
               @refresh-change="refreshChange"
               @search-change="searchChange"
               @search-reset="searchReset"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot-scope="{row}"
                slot="status">
        <el-switch @change="rowSwitchChange(row)"
                   v-model="row.status"
                   active-value="ENABLE"
                   inactive-value="UNENABLE"
                   active-color="#13ce66"
                   inactive-color="#ff4949">
        </el-switch>

      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import {
  getList,
  getPoistion,
  remove,
  update,
  changeStatus,
  add,
} from "@/api/admin/position";
import { mapGetters } from "vuex";

export default {
  data () {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      option: {
        size: 'mini',
        excelBtn: true,
        title:'职位管理',
        tip: false,
        border: true,
        index: true,
        selection: true,
        viewBtn: true,
        height: 'auto',
        // calcHeight: 380,
        dialogType: 'dialog',
        rowKey: 'positionId',
        searchMenuSpan:6,
        column: [
          {
            label: "职位名称",
            prop: "name",
            search: true,
            fixed: true,
            rules: [{
              required: true,
              message: "请输入职位名称",
              trigger: "blur"
            }]
          },
          {
            label: "职位编码",
            prop: "code",
            rules: [{
              required: true,
              message: "请输入职位编码",
              trigger: "blur"
            }]
          },
          {
            label: "状态",
            prop: "status",
            slot: true,
            type: 'switch',
            dicData: [
              {
                label: "关闭",
                value: 'UNENABLE'
              },
              {
                label: "开启",
                value: 'ENABLE'
              },
            ],
          },

          {
            label: "排序",
            prop: "sort",
            type: 'number',
            rules: [{
              required: true,
              message: "请输入排序",
              trigger: "blur"
            }]
          },
          {
            label: "备注",
            prop: "remark",
            type: 'textarea',
            overHidden: true,
            span: 24
          },
          {
            label: "创建时间",
            width: 180,
            prop: "createTime",
            addDisplay: false,
            disabled: true,
          },
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission", 'userInfo']),
    permissionList () {
      return {
        addBtn: this.vaildData(this.permission.positionId_add, true),
        viewBtn: this.vaildData(this.permission.positionId_view, false),
        delBtn: this.vaildData(this.permission.positionId_delete, true),
        editBtn: this.vaildData(this.permission.positionId_update, true)
      };
    },
  },
  methods: {
    rowSwitchChange (row) {
      if (row.status === 'ENABLE') {
        changeStatus(row.userId, 'false');
      } else {
        changeStatus(row.userId, 'true');
      }

    },
    rowSave (row, loading, done) {
      add(row).then(() => {
        loading();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        done();
        ;
      });
    },
    rowUpdate (row, index, loading, done) {
      update(row).then(() => {
        loading();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        done();
        ;
      });
    },
    rowDel (row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.positionId);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    searchReset () {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange (params,done) {
      this.page.currentPage=1;
      this.query = params;
      this.onLoad(this.page, params,done);
    },
    beforeOpen (done, type) {
      if (["edit", "view"].includes(type)) {
        getPoistion(this.form.positionId).then(res => {
          this.form = res.data.data;
        });
      }
      done();

    },
    currentChange (currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange (pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange () {
      this.onLoad(this.page);
    },
    onLoad (page, params = {},done) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        if(done){
          done();
        }
        this.loading = false;
        const data = res.data;
        this.page.total = data.count;
        this.data = data.data
      });
    }
  }
};
</script>

<style>
</style>
